from django.db import models
from myutils.base_model import BaseModel

# Create your models here.


class Category(BaseModel):
    """
    商品类别
    """
    name = models.CharField(max_length=10, verbose_name='名称')
    cid = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, verbose_name='父类别')

    class Meta:
        db_table = 'category'
        verbose_name = '商品类别'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class GoodsChannel(BaseModel):
    """
    商品频道
    """
    group_id = models.IntegerField(verbose_name='组号')
    category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name='顶级商品类别')

    class Meta:
        db_table = 'goods_channel'
        verbose_name = '商品频道'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.category.name


cate_dict = {
    '分组': {
        '频道1': [{'id': '1', 'name': '手机'},
                {'id': '2', 'name': '相机'}],
        '子类': [
            {'id': '3',
             'name': '手机通讯',
             '子类': [
                 {'id': '4', 'name': '手机'},
                 {'id': '5', 'name': '游戏手机'},
                 {'id': '9', 'name': '老人机'},
                 {'id': '10', 'name': '对讲机'}
             ]
             },
            {'id': '6',
             'name': '手机配件',
             '子类': [
                 {'id': '7', 'name': '手机充值卡'},
                 {'id': '8', 'name': '手机电池'}
             ]
             }
        ]
    }
}


class Brand(BaseModel):
    """
    品牌表
    """
    name = models.CharField(max_length=32)
    log = models.ImageField(upload_to='log')
    first_letter = models.CharField(max_length=4)

    class Meta:
        db_table = 'brand'

    def __str__(self):
        return self.name


class SPU(BaseModel):
    """
     Standard Product Unit （标准化产品单元）,SPU是商品信息聚合的最小单位
    """
    name = models.CharField(max_length=100)
    img = models.ImageField(upload_to='spu')
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
    cate1 = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='cate1')
    cate2 = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='cate2')
    cate3 = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='Cate3')
    sales = models.IntegerField(default=0)
    desc = models.TextField()

    class Meta:
        db_table = 'spu'

    def __str__(self):
        return self.name


class Color(BaseModel):
    color_name = models.CharField(max_length=30)
    spu = models.ForeignKey(SPU, on_delete=models.CASCADE)

    class Meta:
        db_table = 'color'

    def __str__(self):
        return self.color_name


class Version(BaseModel):
    version_name = models.CharField(max_length=30)
    spu = models.ForeignKey(SPU, on_delete=models.CASCADE)

    class Meta:
        db_table = 'version'

    def __str__(self):
        return self.version_name


class SKU(BaseModel):
    """
    商品库存量
    """
    name = models.CharField(max_length=50, verbose_name='名称')
    caption = models.CharField(max_length=100, verbose_name='副标题')
    spu = models.ForeignKey(SPU, on_delete=models.CASCADE, verbose_name='商品')
    category = models.ForeignKey(Category, on_delete=models.PROTECT, verbose_name='从属类别')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价')
    cost_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='进价')
    stock = models.IntegerField(default=0, verbose_name='库存')
    default_image_url = models.CharField(max_length=200, default='', null=True, blank=True, verbose_name='默认图片')
    color = models.ManyToManyField(Color)
    version = models.ManyToManyField(Version)


class Order(BaseModel):
    PAY_TYPE_CHOICE = (
        (1, '支付宝'),
        (2, '微信'),
        (3, '银联卡')
    )
    STATUS_CHOICE = (
        (1, '未支付'),
        (2, '已支付'),

    )
    out_trade_no = models.CharField(max_length=64)
    goods = models.ForeignKey(SKU, on_delete=models.CASCADE)
    counter = models.IntegerField(default=0)
    user = models.ForeignKey(to='app01.User', on_delete=models.CASCADE)
    address = models.CharField(max_length=200, null=True, blank=True)
    pay_type = models.IntegerField(default=1, choices=PAY_TYPE_CHOICE)
    status = models.IntegerField(choices=STATUS_CHOICE, default=1)
    trade_no = models.CharField(max_length=64, blank=True, null=True)
    total = models.IntegerField(default=0, null=True, blank=True)






